﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SYcwpb.Helpers;
using System.Data;
using System.Windows.Input;
using SYcwpb.Commands;
using System.Windows.Media;
using SYcwpb.Views;

namespace SYcwpb.ViewModels
{
    public class NofityViewModel : ViewModelBase
    {
        public int MaxCount = 300;
        private SqlDataLayer sqlHelper = new SqlDataLayer();
        int lineid = -1;


        private int _urgencyLevel;
        public int UrgencyLevel
        {
            get
            {
                return _urgencyLevel;
            }
            set
            {
                _urgencyLevel = value;
                OnPropertyChanged("UrgencyLevel");
            }
        }
        private string _fontSize;
        public string FontSize
        {
            set
            {
                _fontSize = value;
                OnPropertyChanged("FontSize");
            }
            get
            {
                return _fontSize;
            }
        }
        private int _notifyType;
        public int NotifyType
        {
            get
            {
                return _notifyType;
            }
            set
            {
                _notifyType = value;
                OnPropertyChanged("NotifyType");
            }
        }
        private string _notifyTypeText;
        public string NotifyTypeText
        {
            get
            {
                return _notifyTypeText;
            }
            set
            {
                _notifyTypeText = value;
                OnPropertyChanged("NotifyTypeText");
            }
        }
        private string _author;
        public string Author
        {
            get
            {
                return _author;
            }
            set
            {
                _author = value;
                OnPropertyChanged("Author");
            }
        }
        private DataTable _notifyTypeSource;
        public DataTable NotifyTypeSource
        {
            get
            {
                return _notifyTypeSource;
            }
            set
            {
                _notifyTypeSource = value;
                OnPropertyChanged("NotifyTypeSource");
            }
        }
        private string _ncContent=string.Empty;
        public string NcContent
        {
            get
            {
                return _ncContent;
            }
            set
            {
                _ncContent = value;
                OnPropertyChanged("NcContent");
            }
        }
        private string _ncDate=string.Empty;
        public string NcDate
        {
            get
            {
                return _ncDate;
            }
            set
            {
                _ncDate = value;
                OnPropertyChanged("NcDate");
            }
        }
        private string _ncUserName = string.Empty;
        public string NcUserName
        {
            get
            {
                return _ncUserName;
            }
            set
            {
                _ncUserName = value;
                OnPropertyChanged("NcUserName");
            }
        }

        private Color _wordCountColor=Color.FromRgb(0,0,0);
        public Color WordCountColor
        {
            get
            {
                return _wordCountColor;
            }
            set
            {
                _wordCountColor = value;
                OnPropertyChanged("WordCountColor");
            }
        }

        private DateTime _deadLineMinDate=DateTime.Now.AddDays(-1);
        public DateTime DeadLineMinDate
        {
            get
            {
                return _deadLineMinDate;
            }
            set
            {
                _deadLineMinDate = value;
                OnPropertyChanged("DeadLineMinDate");
            }
        }
        //DeadLineDate
        private DateTime _deadLineDate=DateTime.Now;
        public DateTime DeadLineDate
        {
            get
            {
                return _deadLineDate;
            }
            set
            {
                _deadLineDate = value;
                OnPropertyChanged("DeadLineDate");
            }
        }
        private bool _deadLineEnable;
        public bool DeadLineEnable
        {
            get
            {
                return _deadLineEnable;
            }
            set
            {
                _deadLineEnable = value;
                OnPropertyChanged("DeadLineEnable");
            }
        }
        private int _enterWordCount=300;
        public int EnterWordCount
        {
            get
            {
                return _enterWordCount;
            }
            set
            {
                _enterWordCount = value;
                OnPropertyChanged("EnterWordCount");
            }
        }


        private Dictionary<int, int> notifyCollections = new Dictionary<int, int>();

        public NofityViewModel()
        {
            lineid = Convert.ToInt32(CONSTDATA.UserModel.Line);
            DeadLineMinDate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
            DeadLineEnable = false;
            UrgencyLevel = 0;
            FontSize ="14";
            InitNotifyType();
        }
        public int GetNotifyTypeID()
        {
            int typeID = -1;
            if (NotifyType == -1)
            {
                MessageBoxHelper.ShowError("请先选择通知类型！");
                return typeID;
            }

            if (notifyCollections.Count > 0)
            {
                if (!notifyCollections.TryGetValue(NotifyType, out typeID))
                {
                    MessageBoxHelper.ShowError("指定类型不存在，请确认！");
                }
            }

            return typeID;
        }
        private void InitNotifyType()
        {
            string sql = "select * from NotifyType";

            DataTable dt = sqlHelper.SelectDt(sql);

            if (null == dt && dt.Rows.Count < 1)
                return;

            NotifyTypeSource = dt;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //cb_notify.Items.Add(dt.Rows[i]["nt_name"].ToString());
                notifyCollections.Add(i, Convert.ToInt32(dt.Rows[i]["ID"]));
            }
        }

        public string NotityInit(int id)
        {
            NcContent = string.Empty;
            Author = string.Empty;
            NcDate = string.Empty;

            string content=InitData(id);

            //EnterWordCount = MaxCount - NcContent.Length;
            return content;
        }
        public void NotifyContentChanged()
        {
            int left = MaxCount - NcContent.Length;
            if (left < 0)
            {
                WordCountColor = Color.FromRgb(255, 0, 0);
            }
            else { WordCountColor = Color.FromRgb(0,0,0); }
            EnterWordCount = left;
        }
        public void SaveNotify(string text,string content)
        {
            try
            {
                if (NcContent.Length > MaxCount)
                {
                    MessageBoxHelper.ShowError("字数超出最大限制!");
                    return;
                }
                if (lineid == -1)
                {
                    MessageBoxHelper.ShowError("本地参数不正确，执行保存操作失败！"); 
                    return;
                }

                if (GetNotifyTypeID() == -1)
                    return;

                int typeID=GetNotifyTypeID();
                string timeNow = DateTime.Now.ToString();

                //通知类型的等级
                int level = UrgencyLevel;
                int fontSize = Convert.ToInt32(FontSize);
                string str = "";

                //string content=
                if (typeID == 3)
                {
                    str = "insert NotifyContent values('" + CONSTDATA.UserModel.UserName + "', " + lineid + ", '" + timeNow + "', " + typeID + ",'" + text + "', " + level + ", " + fontSize + ", '" + DeadLineDate.ToString("yyyy-MM-dd") + "',"+(string.IsNullOrEmpty(content)?"NULL":("'"+content+"'"))+")";
                }
                else
                {
                    str = "insert NotifyContent values('" + CONSTDATA.UserModel.UserName + "'," + lineid + ",'" + timeNow + "'," + typeID + ",'" + text + "'," + level + "," + fontSize + ",NULL,"+(string.IsNullOrEmpty(content)?"NULL":("'"+content+"'"))+")";
                }

                sqlHelper.ExecuteSql(str);


                //MF.tbNotify.Text = rtb_notify.Text;
                Author = CONSTDATA.UserModel.UserName;
                NcDate = timeNow;
                MessageBoxHelper.ShowTips("修改成功!");
            }
            catch (Exception ex)
            {
                MessageBoxHelper.ShowError("修改保存失败！!");
            }
        }
        private string InitData(int typeID)
        {
            string content = string.Empty;
            try
            {
                 

                if (typeID == -1)
                    return content;

                string str = "select top 1 nc_content,nc_date,nc_user_view_name,nc_nt_date,nc_nt_level,nc_nt_fontsize,nc_xmlcontent from NotifyContent where nc_nt_id = " + typeID + " and nc_line = " + lineid + " order by cast(convert(varchar,nc_date,108) as datetime) desc";
                DataTable dt = sqlHelper.SelectDt(str);
                if (dt.Rows.Count == 1)
                {
                    if (typeID == 1)
                    {
                        content = NcContent = dt.Rows[0]["nc_xmlcontent"].ToString();
                    }
                    else
                    {
                        content = NcContent = dt.Rows[0]["nc_content"].ToString();
                    }
                    //content=NcContent = dt.Rows[0]["nc_content"].ToString();
                    NcUserName = dt.Rows[0]["nc_user_view_name"].ToString();
                    NcDate = dt.Rows[0]["nc_date"].ToString();

                    FontSize = dt.Rows[0]["nc_nt_fontsize"].ToString();
                    UrgencyLevel=Convert.ToInt32(dt.Rows[0]["nc_nt_level"]);
                }

                if (typeID == 3)
                {
                    DeadLineEnable = true;
                    if (dt.Rows.Count == 1 && Convert.ToDateTime(dt.Rows[0]["nc_nt_date"].ToString()) >= DeadLineMinDate)
                        DeadLineDate = Convert.ToDateTime(dt.Rows[0]["nc_nt_date"].ToString()).AddSeconds(1);
                }
                else
                {
                    DeadLineEnable = false;
                }
                return content;
            }
            catch (System.Exception ex)
            {
                MessageBoxHelper.ShowError("通知初始化失败");
                return content;
            }

        }
    }
}
